﻿<views:WindowBase
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:views="clr-namespace:PortUtil.Views"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:Converters="clr-namespace:PortUtil.Converters"
    xmlns:viewModels="clr-namespace:PortUtil.ViewModels;assembly=PortUtil.ViewModels"
    xmlns:services="clr-namespace:PortUtil.Services;assembly=PortUtil.Services"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:behaviors="clr-namespace:PortUtil.Behaviors"
    xmlns:portUtil="clr-namespace:PortUtil"
    x:Class="PortUtil.Views.SessionSetupWindow"
    portUtil:WindowEx.ShowIcon="false"
    Title="New session" Height="589.5" Width="413.333" Topmost="True" WindowStartupLocation="CenterOwner"
    ResizeMode="NoResize" ShowInTaskbar="False" Closing="WindowBase_Closing">
    <views:WindowBase.Resources>
        <Converters:HandshakeConverter x:Key="HandshakeConverter" />
        <Converters:StopBitsConverter x:Key="StopBitsConverter" />
        <Converters:FormatConverter x:Key="FormatConverter" />
        <DataTemplate x:Key="FormatItemTemplate" DataType="{x:Type services:Format}">
            <ContentPresenter Content="{Binding Converter={StaticResource FormatConverter}, Mode=OneWay}" />
        </DataTemplate>
    </views:WindowBase.Resources>
    <views:WindowBase.DataContext>
        <Binding Mode="OneWay" Path="SessionSetupViewModel" Source="{StaticResource Locator}" />
    </views:WindowBase.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="45" />
        </Grid.RowDefinitions>
        <Button x:Name="StartButton" Command="{Binding StartCommand}" Content="Start" IsDefault="True"
                HorizontalAlignment="Right" Margin="0,0,99,10" VerticalAlignment="Bottom" Width="75" Grid.Row="1"
                Height="20" IsEnabled="{Binding IsValid, Mode=OneWay}" Click="StartButton_Click" />
        <Button x:Name="CancelButton" Command="{Binding CancelCommand}" Content="Cancel" IsCancel="True"
                HorizontalAlignment="Right" Margin="0,0,10,10" VerticalAlignment="Bottom" Width="75" Grid.Row="1"
                Height="20" Click="CancelButton_Click" />
        <TabControl Margin="5">
            <TabItem Header="Port">
                <TabItem.Resources>
                    <DataTemplate x:Key="StopBitsItemTemplate" DataType="{x:Type services:StopBits}">
                        <ContentPresenter Content="{Binding Converter={StaticResource StopBitsConverter}, Mode=OneWay}" />
                    </DataTemplate>
                    <DataTemplate x:Key="HandshakeItemTemplate" DataType="{x:Type services:Handshake}">
                        <ContentPresenter
                            Content="{Binding Converter={StaticResource HandshakeConverter}, Mode=OneWay}" />
                    </DataTemplate>
                </TabItem.Resources>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="20" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="20" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="120" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
                              Width="150" Height="22" Grid.Row="1" ItemsSource="{Binding SerialPorts}"
                              SelectedItem="{Binding SerialPort, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, ValidatesOnDataErrors=True}" />
                    <Label Content="Serial Port" HorizontalAlignment="Right" Margin="0,8,0,0" VerticalAlignment="Top"
                           RenderTransformOrigin="0.553,0.192" HorizontalContentAlignment="Right" Height="26"
                           Width="64" Grid.Row="1" Grid.RowSpan="2" />
                    <AdornerDecorator Width="150" Grid.Row="3" Height="22" Grid.Column="1" HorizontalAlignment="Left"
                                      Margin="10,10,0,0" VerticalAlignment="Top">
                        <ComboBox
                            IsEditable="True"
                            ItemsSource="{Binding BaudRateItems}"
                            Text="{Binding BaudRate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, ValidatesOnDataErrors=True}">
                            <i:Interaction.Behaviors>
                                <behaviors:TextBoxInputRegExBehaviour RegularExpression="^[0-9.\-]+$" EmptyValue="9600"
                                                                      MaxLength="9" />
                            </i:Interaction.Behaviors>
                        </ComboBox>
                    </AdornerDecorator>
                    <Label Content="Bit Rate" HorizontalAlignment="Right" Margin="0,8,0,0" VerticalAlignment="Top"
                           RenderTransformOrigin="0.553,0.192" HorizontalContentAlignment="Right" Grid.Row="3"
                           Height="26" Width="70" />
                    <ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
                              Width="150" Grid.Row="4" Height="22" ItemsSource="{Binding ParityItems}"
                              SelectedItem="{Binding Parity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <Label Content="Parity" HorizontalAlignment="Right" Margin="0,8,0,0" VerticalAlignment="Top"
                           RenderTransformOrigin="0.553,0.192" HorizontalContentAlignment="Right" Grid.Row="4"
                           Height="26" Width="40" />
                    <CheckBox Content="Data Terminal Ready (DTR)" Grid.Column="1" HorizontalAlignment="Left"
                              Margin="10,15,0,0" VerticalAlignment="Top" Grid.Row="8"
                              IsChecked="{Binding DtrEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                              Height="15" Width="162" />
                    <CheckBox Content="Request to Send (RTS)" Grid.Column="1" HorizontalAlignment="Left"
                              Margin="10,15,0,0" VerticalAlignment="Top" Grid.Row="9"
                              IsChecked="{Binding RtsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                              Height="15" Width="137" />
                    <ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
                              Width="150" Grid.Row="6" Height="22" ItemsSource="{Binding HandshakesItems}"
                              ItemTemplate="{StaticResource HandshakeItemTemplate}"
                              SelectedItem="{Binding Handshake, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <Label Content="Handshake" HorizontalAlignment="Right" Margin="0,8,0,0" VerticalAlignment="Top"
                           RenderTransformOrigin="0.553,0.192" HorizontalContentAlignment="Right" Grid.Row="6"
                           Height="26" Width="81" />
                    <ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
                              Width="150" Grid.Row="5" Height="22" ItemsSource="{Binding StopBitsItems}"
                              ItemTemplate="{StaticResource StopBitsItemTemplate}"
                              SelectedItem="{Binding StopBits, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <Label Content="Stop Bits" HorizontalAlignment="Right" Margin="0,8,0,0" VerticalAlignment="Top"
                           RenderTransformOrigin="0.553,0.192" HorizontalContentAlignment="Right" Grid.Row="5"
                           Height="26" Width="81" />
                    <ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
                              Width="150" Grid.Row="11" Height="22" ItemsSource="{Binding Encodings}"
                              SelectedItem="{Binding Encoding, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <Label Content="Encoding" HorizontalAlignment="Right" Margin="0,8,0,0" VerticalAlignment="Top"
                           RenderTransformOrigin="0.553,0.192" HorizontalContentAlignment="Right" Grid.Row="11"
                           Height="26" Width="81" />
                    <ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
                              Width="150" Height="22" Grid.Row="2" ItemsSource="{Binding DataBitsItems}"
                              SelectedItem="{Binding DataBits, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <Label Content="Data Bits" HorizontalAlignment="Right" Margin="0,8,0,0" VerticalAlignment="Top"
                           RenderTransformOrigin="0.553,0.192" HorizontalContentAlignment="Right" Height="26"
                           Width="64" Grid.Row="2" />
                </Grid>
            </TabItem>
            <TabItem Header="I/O">
                <TabItem.Resources>
                    <DataTemplate x:Key="FormatItemTemplate" DataType="{x:Type services:Format}">
                        <ContentPresenter Content="{Binding Converter={StaticResource FormatConverter}, Mode=OneWay}" />
                    </DataTemplate>
                </TabItem.Resources>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="25" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="25" />
                        <RowDefinition Height="40" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="120" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

                    <ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
                              Width="120" Height="22" Grid.Row="1" ItemsSource="{Binding IntputFormatList}"
                              SelectedItem="{Binding InputFormat, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                              ItemTemplate="{DynamicResource FormatItemTemplate}" />
                    <Label Content="Input format" HorizontalAlignment="Right" Margin="0,8,0,0" VerticalAlignment="Top"
                           RenderTransformOrigin="0.553,0.192" HorizontalContentAlignment="Right" Height="26"
                           Width="103" Grid.Row="1" />
                    <ComboBox Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"
                              Width="120" Height="22" Grid.Row="2" ItemsSource="{Binding OutputFormatList}"
                              SelectedItem="{Binding OutputFormat, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                              ItemTemplate="{DynamicResource FormatItemTemplate}" />
                    <Label Content="Output format" HorizontalAlignment="Right" Margin="0,8,0,0" VerticalAlignment="Top"
                           RenderTransformOrigin="0.553,0.192" HorizontalContentAlignment="Right" Width="103"
                           Grid.Row="2" />
                    <CheckBox Content="Print input to console" Grid.Column="1" HorizontalAlignment="Left"
                              Margin="10,15,0,0" Grid.Row="4" VerticalAlignment="Top"
                              IsChecked="{Binding PrintInputToScreen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <AdornerDecorator Grid.Column="1" HorizontalAlignment="Left" Margin="10,10,0,0"
                                      VerticalAlignment="Top"
                                      Width="120" Height="22" Grid.Row="6">
                        <ComboBox
                            Text="{Binding PushDelay, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, ValidatesOnDataErrors=True}"
                            IsEditable="True">

                            <i:Interaction.Behaviors>
                                <behaviors:TextBoxInputRegExBehaviour RegularExpression="^[0-9.\-]+$" EmptyValue="0"
                                                                      MaxLength="9" />
                            </i:Interaction.Behaviors>

                            <system:Int32>0</system:Int32>
                            <system:Int32>200</system:Int32>
                            <system:Int32>500</system:Int32>
                            <system:Int32>1000</system:Int32>
                            <system:Int32>2000</system:Int32>
                        </ComboBox>
                    </AdornerDecorator>
                    <Label Content="Push delay" HorizontalAlignment="Right" Margin="0,8,0,0" VerticalAlignment="Top"
                           HorizontalContentAlignment="Right" Width="103"
                           Grid.Row="6" />
                    <Label Content="milliseconds" Grid.Column="1" HorizontalAlignment="Left" Margin="135,8,0,0"
                           Grid.Row="6" VerticalAlignment="Top" />
                </Grid>
            </TabItem>
        </TabControl>
        <Button Content="Make default" Height="20" Command="{Binding MakeDefaultCommand}" HorizontalAlignment="Left"
                Margin="10,15,0,10" Grid.Row="1" Width="111" IsEnabled="{Binding IsValid, Mode=OneWay}" />

    </Grid>
</views:WindowBase>